package net.flyinggroup.batch;


import lombok.extern.slf4j.Slf4j;
import net.flyinggroup.util.lang.ThreadUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@SpringBootApplication
public class BatchApplication {
    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }
}

@Slf4j
@Controller
class BatchController {
    @QueryMapping
    Collection<Customer> customers() {
        return List.of(
                new Customer(1, "John"),
                new Customer(2, "Jane")
        );
    }

    @BatchMapping
    Map<Customer, Account> account(List<Customer> customers) {
        log.info("calling account for {} customers", customers.size());
        return customers
                .stream()
                .collect(Collectors.toMap(Function.identity(), customer -> new Account(customer.id())));
    }

//    @SchemaMapping(typeName = "Customer")
//    Account account(Customer customer) {
//        ThreadUtil.sleep(1000);
//        log.info("getting account for customer #{}", customer.id());
//        return new Account(customer.id());
//    }
}


record Customer(Integer id, String name) {
}

record Account(Integer id) {
}